<?php		
/**
 * 
 */

class NfileFace extends FaceBase{

	private $api = null;
	private $conf = null;//;

	private $nohf = array("uploadsave","download","uploadimg","show","getimg","thumbnail");
	private $adhf = array("index",'mufileindex');
	
	/**
	 * 构造函数，初始化插件
	 */
	public function __construct() {
		parent::__construct();
		
		//将标准头加进来，在析构函数中加了标准尾。对于风格的变化，只需要调整标准头标准尾
		$this->api=n_api('nfile');
		$this->conf = n_api("nconfig")->getSite('master',N_EVENT);
		if(!n_api("passport")->islogin()){
			//n_go(n_url("passport",'login'));
		}

		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->header();
		}


	}


	public function index(){
		//print_r($this->api->getDir(12090909));
		//exit;
		$mysql = n_api("mysql")->db001;
		
		$rs = $mysql->select("select * from nfile_mu");
		$this->_html("index",$rs);
	}
	
	public function siteIndex(){
		$param = $this->param("scode:char,dir:uint:ignore,page:page");
		
		
		$mysql = n_api("mysql")->db001;
		$ppid = $_SESSION['ppid'];
		
		
		if(empty($param['dir'])){
			$dirId = 0;
		}else{
			$dirId = $param['dir'];
			$rs['dirid'] = $dirId;
		}
		$rs['scode'] = $param['scode'];
		$sql = "select * from nfile_dir where ppid={$ppid} and scode='{$param['scode']}' and fid={$dirId} and isdel='N'";
		$rs['dir'] = $mysql->select($sql);
		$sql = "select a.*,b.mu_id,b.mime_id,b.md5,b.ext,b.size from nfile_link as a left join nfile_main as b on a.main_id=b.id where dir_id={$dirId} and isdel='N'";
		$rs['file'] = $mysql->select($sql);

		$this->_html("siteIndex",$rs);
	}
	
	public function dirEdit(){
		$param = $this->param("scode:char:ignore,id:uint:ignore,fid:uint:ignore");
		
		if(isset($param['id'])){
			$rs['dir'] = $this->api->getVdir($param['id']);
			if(empty($rs['dir'])){
				throw new PException("找不到目录");
			}
		}elseif(isset($param['fid'])){
			$rs['fdir'] = $this->api->getVdir($param['fid']);
			if(empty($rs['fdir'])){
				throw new PException("找不到父级目录");
			}
		}elseif(isset($param['scode'])){
			$rs['scode'] = $param['scode'];
		}else{
			throw new PException("请求错误");
		}
		$this->_html("dirEdit",$rs);
		
		
	}
	//快速专题
	//二次开发
	public function dirSave(){
		$param = $this->param("name:char,scode:char:ignore,id:uint:ignore,fid:uint:ignore");
		
		$mysql = n_api("mysql")->db001;
		$ppid = $_SESSION['ppid'];
		$now = time();
		if(isset($param['id'])){
			$rs['dir'] = $this->api->getVdir($param['id']);
			if(empty($rs['dir'])){
				throw new PException("找不到目录");
			}
			$mysql->update("nfile_dir",array("name"=>$param['name'],"utime"=>$now),"id={$param['id']}");
		}elseif(isset($param['fid'])){
			$rs['fdir'] = $this->api->getVdir($param['fid']);
			if(empty($rs['fdir'])){
				throw new PException("找不到父级目录");
			}
			$mysql->insert("nfile_dir",array("scode"=>$rs['fdir']['scode'],"name"=>$param['name'],"ppid"=>$ppid,"fid"=>$param['fid'],"utime"=>$now,"ctime"=>$now));
		}elseif(isset($param['scode'])){
			$rs['scode'] = $param['scode'];
			$mysql->insert("nfile_dir",array("scode"=>$rs['scode'],"name"=>$param['name'],"ppid"=>$ppid,"utime"=>$now,"ctime"=>$now));
		}else{
			throw new PException("请求错误");
		}
		$this->_html("dirSave");
		
	}
	public function dirDel(){
		$param = $this->param("id:uint");
		n_api("mysql")->db001->del("nfile_dir",$param['id']);
		$this->_html("dirDel");
	}


	public function fileEdit(){
		$param = $this->param("id:uint");
		
		$mysql = n_api("mysql")->db001;
		$rs = $mysql->get("nfile_link",$param['id']);
		if(empty($rs)){
			throw new PException("找不到文件");
		}
		$this->_html("fileEdit",$rs);
	}
	//快速专题
	//二次开发
	public function fileSave(){
		$param = $this->param("name:char,id:uint,descript:char");
		
		$mysql = n_api("mysql")->db001;
		$now = time();
		$rs = $mysql->get("nfile_link",$param['id']);
		if(empty($rs)){
			throw new PException("找不到文件");
		}
		$mysql->update("nfile_link",array("name"=>$param['name'],"descript"=>$param['descript'],"utime"=>$now),"id={$param['id']}");
		$this->_html("fileSave");
		
	}
	public function fileDel(){
		$param = $this->param("id:uint");
		n_api("mysql")->db001->del("nfile_link",$param['id']);
		$this->_html("fileDel");
	}



	
	public function uploadEdit(){
		$param = $this->param("fid:uint:ignore");
		$param['scode'] = n_api("nsite")->getCurrScode();
		
		$this->_html("uploadEdit",$param);
	}
	public function uploadSave(){
		$param = $this->param("fid:uint:ignore");
		$param['scode'] = n_api("nsite")->getCurrScode();
		$mysql = n_api("mysql")->db001;
		$ppid = $_SESSION['ppid'];
		if(!isset($_FILES['file'])){
			throw new PException("请指定上传变量为file");
		}
		if(0!=$_FILES['pic']['error']){
			throw new PException("上传失败");
		}
		print_r($param);
		if(!empty($param['fid'])){
			$dirid = $param['fid'];
			$dinfo = $mysql->get("nfile_dir",$param['fid']);
			print_r($dinfo);
			if(empty($dinfo)){
				throw new PException("找不到目录");
			}
			$scode = $dinfo['scode'];
			
		}elseif(!empty($param['scode'])){
			$scode = $param['scode'];
			$dirid = 0;
		}else{
			throw new PException("请求错误");
		}


		$file = $_FILES['file'];

		$ext = $this->api->getExt($file['name']);
		$name = $this->api->getName($file['name']);
		$mime = $this->api->getMime($file['tmp_name']);


		$saveRs = $this->api->save($file['tmp_name'],$dirid,$name,$ext);
		n_api("mysql")->db001->insert("nfile_link",array("scode"=>$scode,"dir_id"=>$dirid,"ppid"=>$ppid,"main_id"=>$saveRs['id'],"name"=>$name,"ctime"=>time()));
		echo json_encode(array('上传成功'));
	}


	public function uploadImg(){
		
		$uploadRs = $this->api->uploadSave();
		$mime = $this->api->getMime($uploadRs['mime_id']);
		//exit;
		//print_r($uploadRs);
		//print_r($mime);
		try{
			if('image'!=$mime['type']){
				throw new PException("上传的文件不是图片！");
			}
			$rs = n_state(TRUE, "上传成功");
			$rs['nfile'] = $uploadRs;
			$rs['mime'] = $mime;
			
		}catch(PException $p){
			$rs = n_state(FALSE, $p->getMessage());
		}catch(exception $e){
			$rs = n_state(FALSE, $e->getMessage());
		}
		
		echo json_encode($rs);
	}


	public function uploadVideo(){
		$uploadRs = $this->api->uploadSave();
		$mime = $this->api->getMime($uploadRs['mime_id']);
		//print_r($uploadRs);
		//print_r($mime);
		try{
			if('video'!=$mime['type']){
				throw new PException("上传的文件不是视频！");
			}
			$rs = n_state(TRUE, "上传成功");
			$rs['nfile'] = $uploadRs;
			$rs['mime'] = $mime;
			
		}catch(PException $p){
			$rs = n_state(FALSE, $p->getMessage());
		}catch(exception $e){
			$rs = n_state(FALSE, $e->getMessage());
		}
		echo json_encode($rs);
	}




	public function imgSave(){
		$param = $this->praam("");
		$param = $this->param("fid:uint:ignore,scode:char:ignore");

	}
	public function thumbnail(){
		
 		//exit;
		$im = imagecreate(50,50);
		
		$string = '??';
		
		$bg = imagecolorallocate($im, 200, 200, 200);
		$black = imagecolorallocate($im, 0, 0, 0);
		
		// prints a black "P" in the top left corner
		imagechar($im, 20, 20, 20, $string, $black);
		
		header('Content-type: image/png');
		imagepng($im);
		 		
		exit;
		
	}
	public function show(){
		$_GET['default'] = '/skin/img/m2/bg09.gif';
		$this->getImg();
	}
	public function getImg(){
		$param = $this->param("
								id:uint,
								default:char:ignore,
								size:char:ignore,
								full:char:ignore,
								bg:char:ignore,
								rotate:uint:ignore,
								reversal:char:ignore,
								cut:char:ignore");

		$id = $param['id'];

		$imgInfo = $this->api->get($id);
		
		$mulist = $this->api->getMu();
		$cl = $this->api->getCopyList($id);
		//$randMu = array_rand($cl);
		//print_r($randMu);
		$currentMu = $mulist[$cl[array_rand($cl)]['mu_id']];
		//print_r($imgInfo);
		if(!$imgInfo['state']){
			$filename = N_PLUGINS . $param['default'];
			$mime = $this->api->getMime($imgInfo['finfo']['mime_id']);
		}else{
			$filename = $currentMu['dir'].$imgInfo['path'];
			$mime = array("type"=>"image","subtype"=>"png");
		}
		//echo $filename;
		//exit;
		//header("Content-Type: ".$mime['type'].'/'.$mime['subtype']);
 		//exit;
 		
 		if(!empty($param['size']) || !empty($param['rotate']) || !empty($param['reversal']) || !empty($param['cut'])){
 			
			$iHandle = n_api("common")->img($filename);
			$bg = empty($param['bg'])?'':$param['bg'];
			//先旋转后缩放，旋转后出现尺寸变化的情况
			if(!empty($param['rotate']) && is_numeric($param['rotate'])){
				$iHandle->rotate($param['rotate'],$bg);
			}
			if(!empty($param['reversal'])){
				$iHandle->reversal($param['reversal']);
			}
			if(!empty($param['size'])){
				$size = explode('*',$param['size']);
				
				$sourceSize = $iHandle->getSize();
				$sizeX = is_numeric($size[0])?$size[0]:$sourceSize['width'];
				$sizeY = is_numeric($size[1])?$size[1]:$sourceSize['height'];
				if(!empty($param['bg'])){
					$iHandle->boxResize2($sizeX,$sizeY,$param['bg']); //如果带有bg参数，那么说明是固定背景尺寸等比率缩放
				}elseif(!empty($param['full'])){
					$iHandle->boxResize3($sizeX,$sizeY);					
				}else{
					$iHandle->boxResize($sizeX,$sizeY);
				}
	
			}
			//裁图
			if(!empty($param['cut'])){
				$cut = explode(",", $param['cut']);
				$cut = array_pad($cut,4,0);//存在参数错误的情况。需要补满
				$iHandle->cut($cut[0],$cut[1],$cut[2],$cut[3]);
			}
			$iHandle->iprint();
 		}else{
 			header("Content-Type: ".$mime['type'].'/'.$mime['subtype']);
			echo file_get_contents($filename);
			exit;
 		}
 		
		
	}

	public function muFileIndex(){
		$param = $this->param("id:uint,page:page");
		$mysql = n_api("mysql")->db001;
		$perpage = 20;
		$start = ($param['page']-1)*$perpage;
		$rs['data'] = $mysql->select("select b.*,c.* from nfile_copy as a 
												left join nfile_main as b on a.main_id=b.id
												left join nfile_mime as c on b.mime_id=c.id
												where mu_id={$param['id']}
												order by a.id desc
												limit {$start},{$perpage}");
		$rs['paging'] = n_api("common")->paging(n_url('nfile','muFileIndex',array('id'=>$param['id'])),$param['page'],$mysql->count(),$perpage);
		
		$this->_html("muFileIndex",$rs);
	}



	public function download(){
		$param = $this->param("id:uint");
		
		
		$this->api->download($param['id']);
		/**
		$mysql = n_api("mysql")->db001;
		
		$finfo = $this->api->get($param['id']);

		if(!$finfo['state']){
			header("HTTP/1.0 404 Not Found");
			exit;
		}
		if(empty($finfo['finfo']['name'])){
			$finfo['finfo']['name'] = $finfo['finfo']['md5']; 
		}
		header("Content-type: application/octet-stream;charset=utf8");
		header("Accept-Ranges: bytes");
		header("Accept-Length: {$finfo['finfo']['size']}");
		header("Content-Disposition: attachment; filename={$finfo['finfo']['name']}.{$finfo['finfo']['ext']}");
		echo file_get_contents($finfo['url']);
		exit;
		 * 
		 */

	}


	public function confEdit(){
		$rs = $this->conf;
		
		$this->_html("confEdit",$rs);
	}
	public function confSave(){

		$param = $this->param("
							copynum:uint
		");
		
		$cHandle = n_api("nconfig");
		$data['copynum'] = $param['copynum'];
		$cHandle->pluginsUpdate($data);
		//$this->_html("confSave");
		n_mod("skin")->save("保存成功！");
	}


	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();		
		}
	}
}
